<template>
<div class="login-container">
    <div class="login-form-wrap">
    <div class="login-head">
      <div class="logo"></div>
    </div>
    <!-- 表单 -->
          <el-form :model="form" :rules="rules" status-icon ref="form">
              <el-form-item  prop='mobile'>
                <el-input v-model="form.mobile" placeholder=" 请输入手机号码" ></el-input>
              </el-form-item>

              <el-form-item prop="code">
                <el-input v-model="form.code" placeholder=" 请输入验证码"></el-input>
              </el-form-item>

              <el-form-item prop="isAgree">
                <el-checkbox v-model="form.isAgree">我已经同意霸王条款 ， 并任你们宰割 </el-checkbox>
              </el-form-item>

              <el-form-item>
                <el-button class="login-btn" type="primary" @click="onSubmit">登录</el-button>
              </el-form-item>
          </el-form>


  </div>
</div>
</template>
<script>
import { setToken } from '@/utils/storage'
export default {
  name: 'login',

  data () {
    return {
      form: {
        mobile: '13911111111',
        code: '246810',
        isAgree: false
      },
      rules: {
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1\d{10}$/, message: '请输入正确的手机号', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '验证码不能为空', trigger: ['blur', 'change'] },
          { pattern: /^\d{6}$/, message: '输入正确的验证码', trigger: ['blur', 'change'] }
        ],
        isAgree: [
          {
            validator: (rule, value, callback) => {
              if (value) {
                callback()
              } else {
                callback(new Error('请点蓝色小框框'))
              }
            },
            trigger: 'change'
          }
        ]

      }

    }
  },

  methods: {
    onSubmit () {
      // console.log('submit!')
      this.$refs.form.validate(async (valid) => {
        if (!valid) return
        console.log('校验成功，准备发送axios')
        try {
          const res = await this.$axios({
            // url: 'http://api-toutiao-web.itheima.net/mp/v1_0/authorizations',
            url: '/mp/v1_0/authorizations',
            method: 'POST',
            data: this.form
          })
          console.log(res)
          this.$message({
            message: '恭喜你，登录成功',
            type: 'success'
          })
          setToken(res.data.data.token)
          this.$router.push('/')
          // localStorage.setItem('token', res.data.data.token)
          // console.log(res)
        } catch (error) {
          // console.log(error, '登录失败')
          this.$message.error('登录错了哦')
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
// 铺满整屏
.login-container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: url("~@/assets/login_bg.jpg") no-repeat;
  background-size: cover;
  .login-form-wrap {
    width: 400px;
    padding: 30px 50px 10px;
    background-color: #fff;
    .login-head {
      display: flex;
      justify-content: center;
      .logo {
        width: 200px;
        height: 57px;
        background: url("~@/assets/logo_index.png") no-repeat;
        background-size: contain;
      }
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>
